#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "gps.h"

#define GPS_LEN 512

int gprmc_analysis(char *buff,GPRMC *gprmc);
int set_opt(int fd,int nBits,char nEvent,int nSpeed,int nStop);

int main()
{
	char* gpsdev="/dev/ttySAC3";
	int  gps_fd;
	char gps_buf[GPS_LEN];
	int  nread;
	GPRMC gprmc;
	printf("GPS	id dev/ttySAC3\n");
	gps_fd=open(gpsdev,O_RDONLY);
	if(gps_fd<0)
	{
		printf("can't open gps \n");
		
		return -1;
		exit(-1);
	}
	set_opt(gps_fd,8,'N',9600,1);
	while(1)
	{
		sleep(2);
		nread=read(gps_fd,gps_buf,sizeof gps_buf);
		
		//printf("gps_buf :%s",gps_buf);
		
		memset(&gprmc,0,sizeof gprmc);
		
		gprmc_analysis(gps_buf,&gprmc);
		if(nread>0)
		{
		printf("===========================================\n");
        printf("= GPS stat : %c  A: V:==\n" ,gprmc.pos_state);
        printf("= GPS mode : %c  A: D:==\n" , gprmc.mode);
        printf("=date : 20%02d-%02d-%02d=\n",gprmc.date%100, (gprmc.date%10000)/100,gprmc.date/10000);
        printf("=time : %02d:%02d:%02d=\n",(gprmc.time/10000+8)%24,(gprmc.time%10000)/100,gprmc.time%100);
        printf("=weidu : north:%.3f=\n",(gprmc.latitude/100));
        printf("=jingdu : east:%.3f=\n",(gprmc.longitude/100));
        printf("=sudu: %.3f =\n",gprmc.speed);
        printf("===========================================\n");
		}	
	}
	close(gps_fd);
	return 0;
	
}